﻿using System;
using System.Collections.Generic;
using cn.jpush.api;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;

namespace Pay.Core {
	/// <summary>
	/// 推送消息
	/// </summary>
	public class JPushHelp {
		private string _app_key;
		private string _master_secret;
		/// <summary>
		/// 推送消息
		/// </summary>
		/// <param name="app_key">App key.</param>
		/// <param name="master_secret">Master secret.</param>
		public JPushHelp(string app_key, string master_secret) {
			_app_key = app_key;
			_master_secret = master_secret;
		}

		public void Push(List<JPushMessage> messages) {
			var client = new JPushClient(_app_key, _master_secret);
			foreach (var message in messages) {
				PushPayload payload = new PushPayload();
#if DEBUG
				payload.options.apns_production = false;
#else
				payload.options.apns_production = true;
#endif
				payload.platform = Platform.android_ios();
				payload.audience = Audience.s_alias(message.alias);
				payload.ResetOptionsTimeToLive(60 * 60 * 24);//保留离线消息
				payload.message = Message.content(new { type = message.type, object_id = message.object_id }.ToJson());
				var notifiaction = new Notification().setAlert(message.content);
				notifiaction.AndroidNotification = new AndroidNotification().setTitle(message.title);
				notifiaction.IosNotification = new IosNotification();
				notifiaction.IosNotification.incrBadge(1);
				notifiaction.IosNotification.setSound("default");
				notifiaction.IosNotification.setContentAvailable(true);
				payload.notification = notifiaction.Check();

				try {
					client.SendPush(payload);
				} catch {
				}
			}
		}
		public void Push(string title, string content, int type, long object_id, List<string> alias = null, string jumpUrl=null) {
			var client = new JPushClient(_app_key, _master_secret);
			PushPayload payload = new PushPayload();
#if DEBUG
			payload.options.apns_production = false;
#else
			payload.options.apns_production = true;
#endif
			payload.platform = Platform.android_ios();
			if (alias == null)
				payload.audience = Audience.all();
			else
				payload.audience = Audience.s_alias(alias.ToArray());
			payload.ResetOptionsTimeToLive(60 * 60 * 24);//保留离线消息
			payload.message = Message.content(new { type = type, object_id = object_id, jumpUrl = jumpUrl }.ToJson());
			var notifiaction = new Notification().setAlert(content);
			notifiaction.AndroidNotification = new AndroidNotification().setTitle(title);
			notifiaction.IosNotification = new IosNotification();
			notifiaction.IosNotification.incrBadge(1);
			notifiaction.IosNotification.setSound("default");
			notifiaction.IosNotification.setContentAvailable(true);
			payload.notification = notifiaction.Check();

			try {
				client.SendPush(payload);
			} catch {
			}
		}
	}
	public class JPushMessage {
		public string title { get; set; }
		public string content { get; set; }
		public int type { get; set; }
		/// <summary>
		/// 用户手机或其它openid
		/// </summary>
		public string alias { get; set; }
		public long object_id { get; set; }
	}
}
